<?php 
namespace Admin\Model;
use Think\Model;
class EditContentModel extends Model{
	public $tableName='content';
	protected $patchValidate = true;

	public function contentShow(){
		$cid=I('cid');
		$mid=M('category')->where("cid=$cid")->getField('mid');
		$table=M('model')->where("mid=$mid")->getField('m_table_name');
		$contid=I('contid');
		$contData=M("$table")->where("contid=$contid")->select();
		if($mid){
			$sql="SELECT f_type,f_fild,name FROM d_filed WHERE mid=$mid ORDER BY fid ASC ";
			$allField=M()->query($sql);
			foreach ($allField as $n=>$v) {
				$function=$v['f_type'];
				$field=$v['f_fild'];
				if(method_exists($this, $function)){
					$Data[$n]['html']=$this->$function($field,$cid,$contData);
					$Data[$n]['name']=$v['name'];
				}

			}
			return $Data;
		}
     	
	}

	//编辑
	public function edit(){
		$f=I('flag');
		$mid=I('mid');
		$table=M('model')->where("mid=$mid")->getField('m_table_name');
		//flag 数组转化成字符串
		if(!empty($f)){
			$flag='';
			foreach ($f as $v) {
				$flag.="$v,";
			}
			$flag=mb_substr($flag,0,-1);
		}

     	//添加到文章表
		if($this->create()){
			
			//上传图片文件
			$thumb='';
			// p($_FILES);exit;
			if(!empty($_FILES['thumb']['tmp_name'])){
				$upload= new \Think\Upload();
     			$image=$upload->upload();
     			if(empty($image)){
     			$this->error='图片上传错误';
     			return $image;
     			}
     			$thumb=$image[0]['path'];
     			$this->data['thumb']=$thumb;
			}
			if(empty($_POST['addtime'])){
				$this->data['addtime']=time();
			}
			// p($_POST);exit;
			$this->data['flag']=$flag;
			$this->data['mid']=$mid;
 			
			
			// $this->data['edit']='';
			// p($this->data);exit;
			if($contid=M("$table")->save($this->data)){
				//缩略图处理
				if($thumb){
					$this->thumb($thumb,$contid);
				}
     			
				// p($data);exit;
				return true;
			}else{
				$this->error='添加失败';
				return  false;
			}
		}


	
 
	}
	
	//标题
	public function title($field,$cid,$c){
		$title=$c[0]['title'];
		$html="<input class='form-control' style='width:300px' type='text' name='$field' value='$title'>";
		return $html;
	}
	//栏目
	public function cid($field,$cid,$c){
		$mid=$c[0]['mid'];
		$contid=$c[0]['contid'];
		$cname=M('category')->where("cid=$cid")->getField('cname');
		$html="$cname <input type='hidden' name='cid' value='$cid'><input type='hidden' name='mid' value='$mid'><input type='hidden' name='contid' value='$contid'>";
		return $html;
	}
	//属性
	public function flag($field,$cid,$c){
		$flag=$c[0]['flag'];
		$html='';
		$html.="<label class='checkbox-inline'><input type='checkbox'  name='{$field}[]' value='热门'";
		if(strstr($flag,'热门')){
			$html.="checked=''";
		}
		$html.=">热门</label>";
		$html.="<label class='checkbox-inline'><input type='checkbox'  name='{$field}[]' value='推荐'";
		if(strstr($flag,'推荐')){
			$html.="checked=''";
		}
		$html.=">推荐</label>";
		$html.="<label class='checkbox-inline'><input type='checkbox'  name='{$field}[]' value='图片'";
		if(strstr($flag,'图片')){
			$html.="checked=''";
		}
		$html.=">图片</label>";
		$html.="<label class='checkbox-inline'><input type='checkbox'  name='{$field}[]' value='置顶'";
		if(strstr($flag,'置顶')){
			$html.="checked=''";
		}
		$html.=">置顶</label>";
		$html.="<label class='checkbox-inline'><input type='checkbox'  name='{$field}[]' value='精华'";
		if(strstr($flag,'精华')){
			$html.="checked=''";
		}
		$html.=">精华</label>";
		$html.="<label class='checkbox-inline'><input type='checkbox'  name='{$field}[]' value='站长推荐'";
		if(strstr($flag,'站长推荐')){
			$html.="checked=''";
		}
		$html.=">站长推荐</label>";
		$html.="<label class='checkbox-inline'><input type='checkbox'  name='{$field}[]' value='幻灯片'";
		if(strstr($flag,'幻灯片')){
			$html.="checked=''";
		}
		$html.=">幻灯片</label>";

		
		return $html;
	}
	//seo标题,点击、关键字
	public function input($field,$cid,$c){
		// p($c);
		foreach ($c[0] as $k => $v) {
			if($field==$k){
				$html="<input class='form-control' style='width:300px' type='text' name='$field' value='$v'>";
			}
		}
		
		return $html;
	}	

	//tag标签
	public function tag($field,$cid,$c){
		$tag=$c[0]['tag'];
		$html="<input class='form-control' style='width:300px' type='text' name='$field' value='$tag'>";
		return $html;
	}
	//描述
	public function textarea($field,$cid,$c){
		$textarea=$c[0]['description'];
		$html="<textarea style='width:500px' class='form-control' rows='3' name='$field'>$textarea</textarea>";
		return $html;
	}
	//缩略图
	public function thoumb($field,$cid,$c){
		$thoumb=$c[0]['thumb'];
		$html="<input type='file' name='$field' ><img class='w50 h50' src='".__ROOT__."/$thoumb' >";
		return $html;
	}
	//添加时间
	public function datetime($field,$cid,$c){
		$addtime=$c[0]['addtime'];
		$html="<input class='form-control' style='width:300px' type='text' name='$field' value='$addtime'>";
		return $html;
	}
	public function template($field,$cid,$c){
		$arr=glob('./Template/ducen/*.html');
		$tpl=array();
		foreach ($arr as $k => $v) {
			$tpl[]=basename($v);
		}
		$template=$c[0]['template'];
		$html.="<select style='width:200px' name='$field' class='form-control'>";
		$html.="<option value=''>===请选择====</option>";
		foreach ($tpl as  $v) {
			
			if($template==$field){
				$html.=	"<option value='{$v}' selected=''>{$v}</option>";
			}
			$html.=	"<option value='{$v}'>{$v}</option>";
				
		}
		$html.="</select>";

		return $html;
	}
	public function content($field,$cid,$c){
		
		$content=htmlspecialchars_decode($c[0]['content']);

		$html=<<<str
<script id="container" name="content" type="text/plain">
	$content
				</script>
str;
	return $html;
	}


	///图片处理缩略图
	public function thumb($thumb,$contid){
		$image = new \Think\Image();
		$image->open("{$thumb}");
		$bigPic='upload/78361402817217_350x350.jpg';
		$mediumPic='upload/78361402817217_250x250.jpg';
		$smallPic='upload/78361402817217_150x150.jpg';
		$data1=$image->thumb(150, 150,\Think\Image::IMAGE_THUMB_NORTHWEST)->save($smallPic);
		$data2=$image->thumb(250, 250,\Think\Image::IMAGE_THUMB_NORTHWEST)->save($mediumPic);
		$data3=$image->thumb(350, 350,\Think\Image::IMAGE_THUMB_NORTHWEST)->save($bigPic);
		if(empty($data3)){
			$this->error="缩略图生成错误";
			return false;
		}else{
			M('pic')->add(array('big'=>$bigPic,'medium'=>$mediumPic,'small'=>$smallPic,'contid'=>$contid));
		}

	}













}





 ?>